//
//  FootTableViewCell.m
//  项目A --半糖
//
//  Created by romana on 16/5/28.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "FootTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "FootCollectionViewCell.h"
#import "FootViewController.h"


#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height



@implementation FootTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCollection];
    }
    return self;
}

-(void)createCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 20;
    CGFloat w = ([UIScreen mainScreen].bounds.size.width - 40 - 20 * 3)/4;
    layout.itemSize = CGSizeMake(w, 85);
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 220) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.collectionView];
    [self.collectionView registerClass:[FootCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    
  //  _hud =[[MBProgressHUD alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    
//    [self addSubview:self.hud];
//    self.hud.labelText=@"努力加载中…";
//    
//    [self.hud show:YES];
    
}
// 重写set方法为了刷新数据
-(void)setCc:(ClassModel *)cc
{
    _cc = cc;
    [self.collectionView reloadData];
  //  [self.hud hide:YES];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _cc.channels.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    NSDictionary *dic = _cc.channels[indexPath.item];
    cell.label.text = dic[@"name"];
    NSString *str = dic[@"icon_url"];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str]placeholderImage:[UIImage imageNamed:@"sd_setImageWithURL"]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 跳转
    FootViewController *guideVC = [[FootViewController alloc] init];
    
    NSDictionary *dic = self.cc.channels[indexPath.item];
    guideVC.detailId = dic[@"id"];
    guideVC.titleStr = dic[@"name"];
    [[self viewController].navigationController pushViewController:guideVC animated:YES];
}

//通过该方法找到父视图的controller
-(UIViewController *)viewController{
    for (UIView *next = [self superview]; next; next = [next superview]) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
